package com.shinow.capacitor.plusins.printer;

import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;

@CapacitorPlugin(name = "Printer")
public class PrinterPlugin extends Plugin {

    private Printer implementation = new Printer();

    @PluginMethod
    public void getUsbDevices(PluginCall call) {
        call.resolve(new JSObject() {{
            this.put("UsbDevices", implementation.getUsbDevices(getActivity()));
        }});
    }

    @PluginMethod
    public void printZpl(PluginCall call) {
        String DeviceName = call.getString("DeviceName");
        String Zpl = call.getString("Zpl");

        if (Zpl == null || "".equals(Zpl)) {
            call.reject("参数异常");
        }

        try {
            implementation.printZpl(call, getActivity(), DeviceName, Zpl);
            call.resolve();
        } catch (PrintRunTimeException e) {
            call.reject(e.getMessage());
        }
    }

    @PluginMethod
    public void printImageBase64ToZpl(PluginCall call) {
        String DeviceName = call.getString("DeviceName");
        String Base64 = call.getString("Base64");
        int Width = call.getInt("Width");
        int Height = call.getInt("Height");
        int Density = call.getInt("Density");

        if (Base64 == null || "".equals(Base64)) {
            call.reject("参数异常");
        }

        try {
            implementation.printImageBase64ToZpl(call, getActivity(), DeviceName, Base64, Width, Height, Density);
            call.resolve();
        } catch (PrintRunTimeException e) {
            call.reject(e.getMessage());
        }
    }
}
